package com.car.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.entity.MaintenanceRecord;
import com.car.mapper.MaintenanceRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 保养记录服务
 *
 * @author Car System
 */
@Service
@RequiredArgsConstructor
public class MaintenanceRecordService {

    private final MaintenanceRecordMapper maintenanceRecordMapper;

    /**
     * 分页查询保养记录（带车辆和客户信息）
     */
    public IPage<MaintenanceRecord> page(Integer current, Integer size, String keyword, 
                                        String startTime, String endTime) {
        Page<MaintenanceRecord> page = new Page<>(current, size);
        return maintenanceRecordMapper.selectRecordPageWithDetails(page, keyword, startTime, endTime);
    }

    /**
     * 根据车辆ID查询保养记录
     */
    public List<MaintenanceRecord> listByVehicleId(Long vehicleId) {
        LambdaQueryWrapper<MaintenanceRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaintenanceRecord::getVehicleId, vehicleId)
                .orderByDesc(MaintenanceRecord::getMaintenanceDate);
        return maintenanceRecordMapper.selectList(wrapper);
    }

    /**
     * 根据客户ID查询保养记录
     */
    public List<MaintenanceRecord> listByCustomerId(Long customerId) {
        LambdaQueryWrapper<MaintenanceRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaintenanceRecord::getCustomerId, customerId)
                .orderByDesc(MaintenanceRecord::getMaintenanceDate);
        return maintenanceRecordMapper.selectList(wrapper);
    }

    /**
     * 根据ID查询保养记录
     */
    public MaintenanceRecord getById(Long id) {
        return maintenanceRecordMapper.selectById(id);
    }

    /**
     * 新增保养记录
     */
    public void save(MaintenanceRecord record) {
        maintenanceRecordMapper.insert(record);
    }

    /**
     * 更新保养记录
     */
    public void update(MaintenanceRecord record) {
        maintenanceRecordMapper.updateById(record);
    }

    /**
     * 删除保养记录
     */
    public void delete(Long id) {
        maintenanceRecordMapper.deleteById(id);
    }

    /**
     * 查询本月保养数量
     */
    public Long countThisMonth() {
        LocalDateTime startOfMonth = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        LambdaQueryWrapper<MaintenanceRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(MaintenanceRecord::getMaintenanceDate, startOfMonth);
        return maintenanceRecordMapper.selectCount(wrapper);
    }

    /**
     * 查询本月保养费用
     */
    public BigDecimal sumCostThisMonth() {
        LocalDateTime startOfMonth = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        String startTimeStr = startOfMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        
        List<MaintenanceRecord> records = maintenanceRecordMapper.selectList(
                new LambdaQueryWrapper<MaintenanceRecord>()
                        .ge(MaintenanceRecord::getMaintenanceDate, startOfMonth)
        );
        
        return records.stream()
                .map(MaintenanceRecord::getCost)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 查询月度统计数据
     */
    public List<Map<String, Object>> getMonthlyStatistics(Integer months) {
        return maintenanceRecordMapper.selectMonthlyStatistics(months);
    }

    /**
     * 导出保养记录
     */
    public List<MaintenanceRecord> exportList(String keyword, String startTime, String endTime) {
        Page<MaintenanceRecord> page = new Page<>(1, 10000); // 导出全部数据
        IPage<MaintenanceRecord> result = maintenanceRecordMapper.selectRecordPageWithDetails(
                page, keyword, startTime, endTime);
        return result.getRecords();
    }
}

